# 使用Python 3.11作为基础镜像
FROM python:3.11-slim

# 设置工作目录
WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    wget \
    gnupg \
    ca-certificates \
    libnss3 \
    libnspr4 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    libx11-xcb1 \
    libxcb-dri3-0 \
    libxtst6 \
    xvfb \
    x11-xkb-utils \
    xauth \
    dbus-x11 \
    coreutils \
    findutils \
    util-linux \
    procps \
    net-tools \
    iproute2 \
    openssh-client \
    vim-tiny \
    less \
    sed \
    grep \
    gawk \
    bash \
    bash-completion \
    fonts-wqy-zenhei \
    fonts-wqy-microhei \
    fonts-noto-cjk \
    chromium \
    chromium-driver \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 安装Node.js 18.x
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
    && apt-get update \
    && apt-get install -y nodejs \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 验证安装
RUN node -v && npm -v

# 设置npm源为国内源
RUN npm config set registry https://registry.npmmirror.com

# 安装Playwright和@playwright/mcp
RUN npm install -g --no-update-notifier --no-audit playwright @playwright/mcp@0.0.44

# 安装Python依赖
COPY requirements/base.txt requirements/dev.txt requirements/prod.txt /app/requirements/
RUN pip install --no-cache-dir -r requirements/base.txt

# 安装Playwright Python包和浏览器
RUN pip install --no-cache-dir playwright \
    && playwright install chromium

# 创建符号链接
RUN ln -s /usr/bin/chromium /usr/bin/chromium-browser \
    && ln -s /usr/bin/chromium /usr/bin/google-chrome-stable \
    && ln -s /usr/bin/chromium /usr/bin/google-chrome \
    && ln -s /usr/lib/node_modules/@playwright/mcp/cli.js /usr/local/bin/mcp-cli

# 复制应用代码
COPY . /app/

# 创建必要目录
RUN mkdir -p logs/server logs/mcp_client logs/agent data/database data/tasks data/scenes data/screenshots data/user_data/user_default data/user_data/user_1 data/user_data/user_2 /app/test_reports

# 确保脚本有执行权限
RUN chmod +x /app/scripts/start_mcp/start_browser_and_mcp_with_params.py \
    && chmod +x /app/start_server.py \
    && chmod +x /app/wsgi.py

# 配置环境变量
ENV PYTHONPATH=/app
ENV DISPLAY=:99
ENV INDOCKER=1

# 暴露端口
EXPOSE 5000 5228 8931 8932 8933

# 启动命令
CMD ["python", "/app/start_server.py"]